package ehz.spen.fantasypremierleague.info;

import java.util.ArrayList;
import android.util.Log;
import ehz.spen.fantasypremierleague.constant.Constant;

public class Team {
    private final String TAG = "Team";
    private final int AMOUNT_PLAYERS = 15;
    private final int AMOUNT_MAIN_PLAYERS = 11;

    private int type; // Constant.TEAM_SCHEME

    private ArrayList<Player> playerList;

    public Team() {
        this.playerList = new ArrayList<Player>();
    }

    public boolean setPlayerList(ArrayList<Player> list) {
        if (playerList == null || list == null) {
            Log.e(TAG, "setPlayersList - params null");
            return false;
        }
        if (list.size() != AMOUNT_PLAYERS) {
            Log.e(TAG, "setPlayersList - number of players is wrong");
            return false;
        }

        this.playerList = list;
        return true;
    }

    public ArrayList<Player> getPlayerList() {
        return this.playerList;
    }

    public boolean switchPlayer(Player player1, Player player2) {
        if (playerList == null || player1 == null || player2 == null) {
            Log.e(TAG, "switchPlayers - params are null");
            return false;
        }
        if (!playerList.contains(player1) || !playerList.contains(player2)) {
            Log.e(TAG, "switchPlayers - params are incorrect");
            return false;
        }

        int pos1 = player1.getPositionInTeam();
        int pos2 = player2.getPositionInTeam();
        player1.setPositionInTeam(pos2);
        player2.setPositionInTeam(pos1);

        return true;
    }

    public ArrayList<Player> getSubstitutionList(Player player) {
        ArrayList<Player> list = new ArrayList<Player>();
        if (player.getPosition() == Constant.PLAYER_POSITION_GOALKEEPER) {
            for (int i = 0; i < playerList.size(); i++) {
                if (playerList.get(i).getPosition() == Constant.PLAYER_POSITION_GOALKEEPER) {
                    if (playerList.get(i).getPositionInTeam() != player.getPositionInTeam()) {
                        list.add(playerList.get(i));
                        break;
                    }
                }
            }
        } else if (player.getPosition() == Constant.PLAYER_POSITION_DEFENDER) {
            for (int i = 0; i < playerList.size(); i++) {
                if (playerList.get(i).getPosition() != Constant.PLAYER_POSITION_GOALKEEPER) {
                    list.add(playerList.get(i));
                    return list;
                }
            }
        } else if (player.getPosition() == Constant.PLAYER_POSITION_MIDFIELDER) {
            for (int i = 0; i < playerList.size(); i++) {
                if (playerList.get(i).getPosition() != Constant.PLAYER_POSITION_GOALKEEPER) {
                    // calculate...
                }
            }
        } else if (player.getPosition() == Constant.PLAYER_POSITION_FORWARD) {
            for (int i = 0; i < playerList.size(); i++) {
                if (playerList.get(i).getPosition() != Constant.PLAYER_POSITION_GOALKEEPER) {
                    // calculate...
                }
            }
        }
        return list;
    }
}
